package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.SysRoleMapper;
import com.woniuxy.domain.DataGridView;
import com.woniuxy.pojo.SysRole;
import com.woniuxy.service.RoleService;
import com.woniuxy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    @Override
    public List<SysRole> getAllAvailbleRoles() {
        return roleMapper.getAllAvailbleRoles();
    }
    @Override
    public List<Integer> queryUserRoleIdsByUid(Integer uid) {
        return roleMapper.queryUserRoleIdsByUid(uid);
    }

    @Override
    public DataGridView getRoleListByPage(RoleVo roleVo) {
        Page<SysRole> page= PageHelper.startPage(roleVo.getPage(),roleVo.getLimit());
        List<SysRole> roleList=this.roleMapper.getListByProperty(roleVo);
        return new DataGridView(Long.valueOf(roleList.size()),roleList);
    }
    @Override
    public Integer[] getMenuIdsByRid(Integer rid) {
        return roleMapper.getMenuIdsByRid(rid);
    }

    @Override
    public boolean doAssignMenuByRoleId(Integer rid, Integer[] menuIds) {
        boolean flag=false;
        try {
            this.roleMapper.deleteRoleMenu(rid);
            for(Integer menuId:menuIds){
                Map<String,Integer> params=new HashMap<String,Integer>();
                params.put("roleId",rid);
                params.put("menuId",menuId);
                this.roleMapper.addRoleMenu(params);
            }
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<String> getRolesByUserId(Integer uid) {
        return this.roleMapper.getRolesByUserId(uid);
    }
}
